/**
 *	@author: Francis Germiline S. Veto
 *	date created: December 24, 2005
 *	version: 1.01
 * 	change history:
 *		Jan. 8, 2006:
 *			- kit: added setAutoCommit(), commit(), and rollback()
 *		Feb. 5, 2006:
 *			- kit: turned executeQuery() call in insert() into executeUpdate()
 */

package text.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

public class DBConnector_access {
	
	private Connection con; 
	
	public DBConnector_access()	{	
		con = null;
	}
		
	public void connect() {
		try {
			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
			con = DriverManager.getConnection("jdbc:odbc:TExt DB");
		}catch(Exception e){System.out.println("Cannot connect to DB");}
	}
	
	public ResultSet select(String sQuery)	{
		try{
			Statement stat = con.createStatement();
			ResultSet rs = stat.executeQuery(sQuery);
			return rs;
		}catch(Exception e)	{
			e.printStackTrace();
			System.out.println("Select Error!");
		}
		return null; 
	}
	
	public void insert(String sQuery)	{
		try	{
			Statement stat = con.createStatement();
			int temp = stat.executeUpdate(sQuery);
		}	catch(Exception e) {
			System.out.println("Insert Error!");
		}
		
	}
	
	public boolean update(String sQuery)	{
		try {
			Statement stat = con.createStatement();
			int temp = stat.executeUpdate(sQuery);
			return true;
		}	catch(Exception e)	{
			System.out.println("Update Error");
		}
		return false; //if program reaches here, then update statement was false.
	}
	
	public boolean isClosed() {
		try {
			return con.isClosed();
		} catch(Exception e) {}
		return false;
	}
	
	public void close()	{
		try{
			con.close();
		}catch(Exception e){}
	}
	
	/**
	 * Allows the creation of a more customized SQL statement.
	 *
	 * Customizes the SQL statement settings according to the parameters.
	 */
	public Statement createStatement(int nResultSetType, int nResultSetConcurrency) {
		try {
			return con.createStatement(nResultSetType, nResultSetConcurrency);
		} catch(Exception e) {}		
		return null;
	}
	
	/**
	 * Toggles the autocommit state to the specified flag parameter.
	 *
	 * Should be used for multiple sql statements that must be treated
	 * atomically.
	 */
	public void setAutoCommit (boolean isOnAutoCommit) {
		try {
			con.setAutoCommit(isOnAutoCommit);
		} catch(Exception e) {
			System.err.println("Error: Cannot change AutoCommit state");
		}
	}
	
	/**
	 * Commits changes manually.
	 * Should ONLY be used when autoCommit is turned off.
	 *
	 * @see setAutoCommit(boolean)
	 */
	public void commit() {
		try {
			con.commit();
		} catch(Exception e) {}
	}
	
	/**
	 * Rolls back changes manually.
	 * Should ONLY be used when autoCommit is turned off.
	 *
	 * Automatically reactivates autoCommit after rollback.
	 *
	 * @see setAutoCommit(boolean)
	 */
	public void rollback() {
		try {
			con.rollback();
			if(!con.getAutoCommit())
				con.setAutoCommit(true);
		} catch(Exception e) {}		
	}
}